<?php
/**
 * Created by PhpStorm.
 * User: moko1988
 * Date: 16/01/016
 * Time: 下午4:58
 */

namespace Admin\Controller;
use Think\Controller;

class PictureCategoryController extends Controller {


    /*
     * 列表
     */
    public function lists(){

        $this->isAjaxLogin();//检查是否登录

        $PictureCategoryModel = M('PictureCategory');

        $result = $PictureCategoryModel->order('rank desc')->select();

        if($result !== false){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据查询失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*保存*/
    public function save(){

        $this->isAjaxLogin();//检查是否登录

        $PictureCategoryModel = M('PictureCategory');

        $id     = I('post.id/d');
        $name   = I('post.name');
        $size_w = I('post.width');
        $size_h = I('post.height');
        $key    = I('post.key');
        $litpic = I('post.litpic');

        $data['name']   = $name;
        $data['size_w'] = $size_w;
        $data['size_h'] = $size_h;
        $data['key']    = $key;
        $data['litpic'] = $litpic;

        /*修改*/
        if($id){

            $result = $PictureCategoryModel->where('id='.$id)->save($data);

            if($result !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据更新失败，请稍后在试！";
            }

        }
        /*添加*/
        else{

            $data['rank']  = $PictureCategoryModel->count() + 1;

            $lastid = $PictureCategoryModel->add($data);

            if($lastid !== false){

                $ajaxReturn['code'] = 1;
                $ajaxReturn['msg']  = "SUCCESS";

            }else{

                $ajaxReturn['code'] = 2;
                $ajaxReturn['msg']  = "数据添加失败，请稍后在试！";
            }
        }

        $this->ajaxReturn($ajaxReturn);

    }

    /*
     * 详情
     */
    public function infos (){

        $this->isAjaxLogin();

        $id = I('post.id/d');

        $PictureCategoryModel = M('PictureCategory');

        $result = $PictureCategoryModel->where('id='.$id)->find();

        if($result){

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
            $ajaxReturn['data'] = $result;

        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "查询数据失败！";
        }

        $this->ajaxReturn($ajaxReturn);
    }

    /*删除*/
    public function delete(){

        $this->isAjaxLogin();//检查是否登录

        $id = I('post.id/d');

        $PictureCategoryModel = M('PictureCategory');
        $PictureModel = M('Picture');

        $PictureModel->where('cid='.$id)->delete();
        $PictureCategoryModel->where('id='.$id)->delete();

        $ajaxReturn['code'] = 1;
        $ajaxReturn['msg']  = "SUCCESS";

        $this->ajaxReturn($ajaxReturn);

    }


    /*
     * 排序
     */
    public function rank (){

        $thisid = I('post.id');
        $type   = I('post.type');
        $and    = $type == 1 ? '>' : '<';
        $order  = $type == 1 ? 'asc' : 'desc';

        $PictureCategoryModel = M('PictureCategory');

        $thisrank   = $PictureCategoryModel->where('id='.$thisid)->getField('rank');

        $nextinfo   = $PictureCategoryModel->where('rank'.$and.$thisrank)->order('rank '.$order)->getField('id,rank');
        $nextid     = array_keys($nextinfo)[0];
        $nextrank   = array_values($nextinfo)[0];

        if($thisrank && $nextid && $nextrank){

            $PictureCategoryModel-> where('id='.$thisid)->setField('rank',$nextrank);
            $PictureCategoryModel-> where('id='.$nextid)->setField('rank',$thisrank);

            $ajaxReturn['code'] = 1;
            $ajaxReturn['msg'] = "SUCCESS";
        }else{

            $ajaxReturn['code'] = 2;
            $ajaxReturn['msg'] = "数据更新失败！";
        }

        $this->ajaxReturn($ajaxReturn);

    }


}